package com.zeny.edu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeny.edu.common.EduException;
import com.zeny.edu.eduservice.entity.EduChapter;
import com.zeny.edu.eduservice.entity.dto.EduChapterDto;
import com.zeny.edu.eduservice.entity.dto.EduVideoDto;
import com.zeny.edu.eduservice.mapper.EduChapterMapper;
import com.zeny.edu.eduservice.service.EduChapterService;
import com.zeny.edu.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zeny
 * @since 2020-03-05
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * @Description 根据课程ID删除章节
     * @Date 2020/3/5 0005 13:13
     * @param id
     * @return void
     **/
    @Override
    public void deleteChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        baseMapper.delete(wrapper);
    }

    /**
     * @Description 根据课程ID查询章节和小节
     * @Date 2020/3/5 0005 14:26
     * @param courseId 
     * @return java.util.List<com.zeny.edu.eduservice.entity.dto.EduChapterDto>
     **/
    @Override
    public List<EduChapterDto> getChapterVideoListByCourseId(String courseId) {
        List<EduChapterDto> chapterDtoList = new ArrayList<>();
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByAsc("sort");
        //查询章节
        //根据课程ID查询章节
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);
        //根据课程ID查询小节
        List<EduVideoDto> eduVideoDtoList = eduVideoService.selectVideoListByCourseId(courseId);
        for (EduChapter eduChapter : eduChapterList) {
            EduChapterDto eduChapterDto = new EduChapterDto();
            BeanUtils.copyProperties(eduChapter, eduChapterDto);
            //章节添加到返回列表
            chapterDtoList.add(eduChapterDto);
            //查询小节并封装在章节中
            String chapterId = eduChapter.getId();
            for (EduVideoDto eduVideoDto : eduVideoDtoList) {
                if (chapterId.equals(eduVideoDto.getChapterId())) {
                    eduChapterDto.getChildren().add(eduVideoDto);
                }
            }
        }
        return chapterDtoList;
    }

    /**
     * @Description 删除章节
     * @Date 2020/3/5 0005 19:21
     * @param chapterId
     * @return boolean
     **/
    @Override
    public boolean removeChapterById(String chapterId) {
        boolean isExist = eduVideoService.isChapterExistVideo(chapterId);
        if (isExist) {
            throw new EduException(20002, "该章节下存在小节，无法删除");
        }
        int result = baseMapper.deleteById(chapterId);
        return result > 0;
    }
}
